#-*- coding:utf-8 -*-
import cv2
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
import time

ellipse = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 9))

extraList = []

def get_dict():
	f = open('column.csv', 'r')
	#f.seek(3)
	lines = f.readlines()
	res = {}
	for line in lines:
		data = line.split(",")
		index = int(data[0])
		content = data[1]
		res[index] = content
	return res

#(dstSize=(24, 24))
def imageProcess(img, dstSize):
	ret, thresh = cv2.threshold(img, 30, 255, 0)
	dilate = cv2.dilate(thresh, ellipse, iterations=2)
	erode = cv2.erode(dilate, ellipse, iterations=2)
	frame = cv2.resize(erode, dstSize, interpolation=cv2.INTER_CUBIC)
	gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	frame = cv2.normalize(gray, frame, 0, 1, cv2.NORM_MINMAX)
	return frame

def stop(flags, l):
	time.sleep(l)
	flags[0] = False
	flags[1] = True

def getLabel(x):
	initial = np.zeros([30])
	initial[x] = 1
	return initial

def augument(src, prefix, dstdir, epoch):
	datagen = ImageDataGenerator(rotation_range=3, width_shift_range=0.01, height_shift_range=0.03, shear_range=0.02,  zoom_range=0.1, horizontal_flip=False, fill_mode='nearest')
	imgList = []
	x = src.shape[0]
	y = src.shape[1]
	img = np.reshape(src, (x, y, 1))
	img = cv2.normalize(img, img, 0, 1, cv2.NORM_MINMAX)
	imgList.append(img)
	imgArray = np.array(imgList)
	i = 0
	for batch in datagen.flow(imgArray, batch_size=len(imgArray), save_to_dir=dstdir, save_prefix=prefix, save_format='jpg'):
		i += 1
		if i >= epoch:
			break
